from typing import List
from thrift.transport import TSocket, TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer

from ocr.api import OcrService
from manager import ocr_img


class OcrServiceHandle(OcrService.Iface):
    def __init__(self) -> None:
        super().__init__()

    def ocr_img(self, img: str) -> List[List[str]]:
        result = ocr_img(img)
        if result != None:
            return result
        else:
            return []


if __name__ == '__main__':
    handler = OcrServiceHandle()
    processor = OcrService.Processor(handler=handler)

    transport = TSocket.TServerSocket(port=3000)
    tfactory = TTransport.TFramedTransportFactory()
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()

    server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)

    print('OcrService start ...')
    server.serve()
    print('OcrService Stoped.')
